package cn.weforward.task.weforward;


import javax.annotation.Resource;

import cn.weforward.common.ResultPage;
import cn.weforward.common.util.StringUtil;
import cn.weforward.common.util.TransResultPage;
import cn.weforward.framework.ApiException;
import cn.weforward.framework.WeforwardMethod;
import cn.weforward.framework.WeforwardMethods;
import cn.weforward.framework.doc.DocMethods;
import cn.weforward.framework.util.ValidateUtil;
import cn.weforward.protocol.client.util.IdBean;
import cn.weforward.protocol.doc.annotation.DocAttribute;
import cn.weforward.protocol.doc.annotation.DocMethod;
import cn.weforward.protocol.doc.annotation.DocParameter;
import cn.weforward.protocol.support.datatype.FriendlyObject;
import cn.weforward.task.Tag;
import cn.weforward.task.TaskService;
import cn.weforward.task.impl.SimpleTag;
import cn.weforward.task.impl.SimpleTask;
import cn.weforward.task.weforward.view.DeleteTagParam;
import cn.weforward.task.weforward.view.SearchTagParam;
import cn.weforward.task.weforward.view.SimpleTaskView;
import cn.weforward.task.weforward.view.TagParam;
import cn.weforward.task.weforward.view.TagView;
import cn.weforward.task.weforward.view.UpdateTagParam;

/**  
 * 标签接口
 *
 */
@DocMethods(index = 300)
@WeforwardMethods
public class TagMethods{
	
	@Resource
	TaskService m_Service;
	
	@WeforwardMethod
	@DocMethod(description = "创建一个标签",index = 1)
	public TagView create(TagParam params) throws ApiException{
		String name = params.getName();
		int state = params.getState();
		ValidateUtil.isEmpty(name, "标签名字不能为空");
		ValidateUtil.isEmpty(state, "标签状态不能为空");
		if(state != 1 || state !=2) {
			//如果状态不为1或2，则默认给一个2
			state = 2;//默认标签显示状态
		}
		
		
		Tag tag = m_Service.createTag(name, state);
		return TagView.valueOf(tag);
	}
	
//	@WeforwardMethod
//	@DocMethod(description = "搜索标签",index = 2)
//	public ResultPage<TagView> search(SearchTagParam params) throws ApiException{
//		ResultPage<Tag> rp = m_Service.searchTags(params.getKeywords(), params.getOption());
//		
//		return new TransResultPage<TagView, Tag>(rp) {
//			@Override
//			protected TagView trans(Tag src) {
//				// TODO Auto-generated method stub
//				return TagView.valueOf(src);
//			}
//			
//		};
//	}
	
	@WeforwardMethod
	@DocParameter({@DocAttribute(name = "page", type = Integer.class, necessary = false, description = "第几页"),
					@DocAttribute(name = "pageSize", type = Integer.class, necessary = false, description = "一页数据大小")})
	@DocMethod(description = "列出所有标签",index = 2)
	public ResultPage<TagView> searchAllTags() throws ApiException{
		ResultPage<SimpleTag> rp = m_Service.searchAllTags();
		
		return new TransResultPage<TagView, SimpleTag>(rp) {
			@Override
			protected TagView trans(SimpleTag src) {
				// TODO Auto-generated method stub
				return TagView.valueOf(src);
			}
			
		};
	}
	
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", type = String.class, necessary = true, description = "标签id"))
	@DocMethod(description = "查询标签",index = 3)
	public TagView get(FriendlyObject params) throws ApiException {
		ValidateUtil.isEmpty(params.getString("id"), "标签id不能为空");
		return TagView.valueOf(check(m_Service.getTag(params.getString("id"))));
	}
	
	
	
	@WeforwardMethod
	@DocMethod(description = "修改标签",index = 5)
	public TagView update(UpdateTagParam params) throws ApiException{
		Tag tag = m_Service.getTag(params.getId());
		
		ValidateUtil.isNull(tag, "该标签id不存在");
		
		String name = params.getName();
		if(!StringUtil.isEmpty(name)) {
			tag.setName(name);
		}
		int state = params.getState();
		
		if(state==1) {
			tag.hide();
		}else if(state == 2) {
			tag.show();
		}
		
		
		return TagView.valueOf(tag);
	}
	
	@WeforwardMethod
	@DocParameter(@DocAttribute(name = "id", type = String.class, necessary = true, description = "标签id"))
	@DocMethod(description = "删除标签",index = 6)
	public TagView remove(DeleteTagParam params) throws ApiException{
		Tag tag = m_Service.getTag(params.getId());
		ValidateUtil.isNull(tag, "该标签id不存在");
		m_Service.removeTag(params.getId());
		return TagView.valueOf(tag);
	}
	
	private Tag check(Tag tag) {
		if (null == tag) {
			return null;
		}

		return tag;
	}

}
